import axios from "axios";
import router from "@/router";

import Cookies from "js-cookie";
import qs from "qs";
import isPlainObject from "lodash/isPlainObject";
import { clearLoginInfo } from "./index";

const http = axios.create({
  baseURL: "http://gather.dfinfo.net.cn/gather-admin",
  timeout: 1000 * 180,
  withCredentials: true
});


/**
 * 请求拦截
 */
http.interceptors.request.use(
  config => {
    config.headers["Accept-Language"] = Cookies.get("language") || "zh-CN";
    config.headers["token"] = Cookies.get("token") || "";
    // 默认参数
    var defaults = {};
    // 防止缓存，GET请求默认带_t参数
    if (config.method === "get") {
      config.params = {
        ...config.params,
        ...{ _t: new Date().getTime() }
      };
    }
    if (isPlainObject(config.params)) {
      config.params = {
        ...defaults,
        ...config.params
      };
    }
    if (isPlainObject(config.data)) {
      config.data = {
        ...defaults,
        ...config.data
      };
      if (
        /^application\/x-www-form-urlencoded/.test(
          config.headers["content-type"]
        )
      ) {
        config.data = qs.stringify(config.data);
      }
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);



/**
 * 响应拦截
 */
// http.interceptors.response.use(response => {
//   if (response.data.code === 401 || response.data.code === 10001) {
//     clearLoginInfo()
//     router.replace({ name: 'login' })
//     return Promise.reject(response.data.msg)
//   }
//   return response
// }, error => {
//   console.error(error)
//   return Promise.reject(error)
// })


http.get("/sys/user/info").then(res => {
  console.log(res);
});

// const httpGet = http
//   .get("/newborn/gather/indexStatistics", {
//     params: {
//       startDate: '2020-08-05',
//       endDate: '2021-07-05'
//     }
//   })
//   .then(res => {
//     console.log(res.data.data);
//   });

export default http;



